home *** CD-ROM | disk | FTP | other *** search
/ Ray Dream Studio 5 / Ray Dream.iso / pc / DreamSDK / Windows / SAMPLES / CAMERA / CAMC / COMCAMC.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1997-07-11  |  10.1 KB  |  290 lines

  1. /* $Id: COMCamC.cpp 1.4 1997/04/11 02:32:09 damien Exp $ */
  2.  
  3. // Copyright (c) 1995, Ray Dream, Inc. All rights reserved.
  4.  
  5. ////////////////////////////////////////////////////////////////////////
  6. //   Second Camera Example : Conical Camera                           //
  7. //--------------------------------------------------------------------//
  8. //   Implementation of the Conical Camera Interface                   //
  9. //////////////////////////////////////////////////////////////////////// 
  10.  
  11. #include "math.h"
  12.  
  13. #ifndef __COMCAMC__
  14. #include "COMCAMC.h"
  15. #endif
  16.        
  17. #ifndef __CAMCDLL__
  18. #include "CAMCDLL.h"
  19. #endif 
  20.  
  21. #ifndef __3DCOFAIL__
  22. #include "3DCoFail.h"
  23. #endif 
  24.  
  25. NUM3D kQuickFocal      = 50.0;                       // Focal = 50 mm
  26. NUM3D kQuickmmTo3DUnit = 10.0/1016.0;                // Conversion mm to 3D Unit
  27. NUM3D kQuick288        = 288.0;                      // Conversion 3D Unit to Pt Unit 
  28.          
  29. // Tool to get the Global Coordinates from the Local Coordinates 
  30. void LocalToGlobal(TRANSFORM3D* transform,VECTOR3D* LocalPos,VECTOR3D* GlobalPos) {
  31.   // Axes rotations
  32.   (*GlobalPos)[0]=transform->fR.fix*(*LocalPos)[0]
  33.                   +transform->fR.fjx*(*LocalPos)[1]
  34.                   +transform->fR.fkx*(*LocalPos)[2];
  35.   (*GlobalPos)[1]=transform->fR.fiy*(*LocalPos)[0]
  36.                   +transform->fR.fjy*(*LocalPos)[1]
  37.                   +transform->fR.fky*(*LocalPos)[2];
  38.   (*GlobalPos)[2]=transform->fR.fiz*(*LocalPos)[0]
  39.                   +transform->fR.fjz*(*LocalPos)[1]
  40.                   +transform->fR.fkz*(*LocalPos)[2];
  41.   // Unit conversion : Points Units to 3D Units 
  42.   (*GlobalPos)[0] /= 288.0;  
  43.   (*GlobalPos)[1] /= 288.0;  
  44.   (*GlobalPos)[2] /= 288.0;  
  45.   // Origin translation
  46.   (*GlobalPos)[0] += transform->fT[0];
  47.   (*GlobalPos)[1] += transform->fT[1];
  48.   (*GlobalPos)[2] += transform->fT[2];
  49.   }
  50.  
  51. // Tool to get the Global Coordinates from the Local Coordinates for a Vector
  52. void LocalToGlobalVector(TRANSFORM3D* transform,VECTOR3D* LocalPos,VECTOR3D* GlobalPos) {
  53.   // Axes rotations
  54.   (*GlobalPos)[0]=transform->fR.fix*(*LocalPos)[0]
  55.                   +transform->fR.fjx*(*LocalPos)[1]
  56.                   +transform->fR.fkx*(*LocalPos)[2];
  57.   (*GlobalPos)[1]=transform->fR.fiy*(*LocalPos)[0]
  58.                   +transform->fR.fjy*(*LocalPos)[1]
  59.                   +transform->fR.fky*(*LocalPos)[2];
  60.   (*GlobalPos)[2]=transform->fR.fiz*(*LocalPos)[0]
  61.                   +transform->fR.fjz*(*LocalPos)[1]
  62.                   +transform->fR.fkz*(*LocalPos)[2];
  63.   // Unit conversion : Points Units to 3D Units 
  64.   (*GlobalPos)[0] /= 288.0;  
  65.   (*GlobalPos)[1] /= 288.0;  
  66.   (*GlobalPos)[2] /= 288.0;  
  67.   }
  68.  
  69. // Tool to get the Local Coordinates from the Global Coordinates             
  70. void GlobalToLocal(TRANSFORM3D* transform,VECTOR3D* GlobalPos,VECTOR3D* LocalPos) {
  71.   // Origin translation
  72.   (*GlobalPos)[0] -= transform->fT[0];
  73.   (*GlobalPos)[1] -= transform->fT[1];
  74.   (*GlobalPos)[2] -= transform->fT[2];
  75.    // Unit conversion : 3D Units to Points Units
  76.   (*GlobalPos)[0] *= 288.0;  
  77.   (*GlobalPos)[1] *= 288.0;  
  78.   (*GlobalPos)[2] *= 288.0;  
  79.  
  80.   // Axes rotations
  81.   (*LocalPos)[0]=transform->fR.fix*(*GlobalPos)[0]
  82.                   +transform->fR.fiy*(*GlobalPos)[1]
  83.                   +transform->fR.fiz*(*GlobalPos)[2];
  84.   (*LocalPos)[1]=transform->fR.fjx*(*GlobalPos)[0]
  85.                   +transform->fR.fjy*(*GlobalPos)[1]
  86.                   +transform->fR.fjz*(*GlobalPos)[2];
  87.   (*LocalPos)[2]=transform->fR.fkx*(*GlobalPos)[0]
  88.                   +transform->fR.fky*(*GlobalPos)[1]
  89.                   +transform->fR.fkz*(*GlobalPos)[2];
  90.   }
  91.  
  92. // Tool to get the Local Coordinates from the Global Coordinates for a Vector
  93. void GlobalToLocalVector(TRANSFORM3D* transform,VECTOR3D* GlobalPos,VECTOR3D* LocalPos) {
  94.    // Unit conversion : 3D Units to Points Units
  95.   (*GlobalPos)[0] *= 288.0;  
  96.   (*GlobalPos)[1] *= 288.0;  
  97.   (*GlobalPos)[2] *= 288.0;  
  98.  
  99.   // Axes rotations
  100.   (*LocalPos)[0]=transform->fR.fix*(*GlobalPos)[0]
  101.                   +transform->fR.fiy*(*GlobalPos)[1]
  102.                   +transform->fR.fiz*(*GlobalPos)[2];
  103.   (*LocalPos)[1]=transform->fR.fjx*(*GlobalPos)[0]
  104.                   +transform->fR.fjy*(*GlobalPos)[1]
  105.                   +transform->fR.fjz*(*GlobalPos)[2];
  106.   (*LocalPos)[2]=transform->fR.fkx*(*GlobalPos)[0]
  107.                   +transform->fR.fky*(*GlobalPos)[1]
  108.                   +transform->fR.fkz*(*GlobalPos)[2];
  109.   }
  110.  
  111.  
  112.  
  113.  
  114. #undef INTERFACE
  115. #define INTERFACE ConicCamera
  116. // Constructor / Destructor of the C++ Object :
  117. ConicCamera::ConicCamera() {
  118.   fCRef=0;  // Reference counter
  119.   // Rotation Matrix initialized to identity :
  120.   fTransform.fR.fix=fTransform.fR.fjy=fTransform.fR.fkz=1.0;
  121.   fTransform.fR.fjx=fTransform.fR.fkx=0.0;
  122.   fTransform.fR.fiy=fTransform.fR.fky=0.0;
  123.   fTransform.fR.fiz=fTransform.fR.fjz=0.0;
  124.   // Translation Vector initialized to zero  :
  125.   fTransform.fT[0]=fTransform.fT[1]=fTransform.fT[2]=0.0;
  126.   // Initial Zoom Coefficient
  127.   fData.fZoomCoef=10;
  128.   fQuickZoom =((NUM3D)fData.fZoomCoef)/10.0; // fZoomCoef = 10x Zoom
  129.   fCoef=kQuick288*kQuickmmTo3DUnit; // Convert mm in Point Unit
  130.   }
  131.   
  132. ConicCamera::~ConicCamera() {
  133.   global_count_Obj--; 
  134.   }
  135.   
  136. // IUnknown Interface :
  137. HRESULT ConicCamera::QueryInterface(THIS_ REFIID riid,LPVOID FAR* ppvObj) {
  138.   *ppvObj=NULL;
  139.   
  140.   // The ConicCamera knows the interfaces of the parent Objects
  141.   if (IsEqualIID(riid, IID_IUnknown))
  142.     *ppvObj=(LPVOID)this;
  143.   else if (IsEqualIID(riid, IID_I3DExCamera))
  144.     *ppvObj=(LPVOID)this;
  145.   else if (IsEqualIID(riid, IID_I3DExDataExchanger))
  146.     *ppvObj=(LPVOID)this;
  147.   else if (IsEqualIID(riid, IID_I3DExtension))
  148.     *ppvObj=(LPVOID)this;
  149.     
  150.   // we must add reference if we return an interface
  151.   if (*ppvObj!=NULL) {
  152.     ((LPUNKNOWN)*ppvObj)->AddRef();
  153.     return NOERROR;
  154.     }
  155.   else {
  156.     return ResultFromScode(E_NOINTERFACE);
  157.     }
  158.   }
  159.  
  160. ULONG ConicCamera::AddRef(THIS) {
  161.   return fCRef++;
  162.   }
  163.   
  164. ULONG ConicCamera::Release(THIS) {
  165.   ULONG UnreleaseObject=fCRef--;
  166.   
  167.   if (fCRef==0)
  168.      delete this; // No reference left so delete the object
  169.   
  170.   return UnreleaseObject;
  171.    // Use local variable because if the object is destructed
  172.    // fCRef do not exist
  173.   }
  174.   
  175. // I3DExtension methods :
  176. I3DExtension* ConicCamera::Clone(THIS) {
  177.   ConicCamera* theClone = new ConicCamera;
  178.   if (theClone) {
  179.     theClone->AddRef();
  180.     theClone->fTransform=fTransform; // Copy all the Data
  181.     theClone->fData     =fData;
  182.     theClone->fQuickZoom=fQuickZoom;
  183.     theClone->fCoef     =fCoef;
  184.     }                               
  185.   return theClone;
  186.   }
  187.      
  188. HRESULT ConicCamera::ShellUtilitiesInit(THIS_ IShUtilities* shellUtilities) {
  189.   InitCoFailure(shellUtilities);
  190.   return NOERROR;
  191.   }
  192.  
  193. // I3DExDataExchanger methods :
  194. ExtensionDataMap* ConicCamera::GetExtensionDataMap(THIS) {
  195.   return NULL;
  196.   }               
  197.   
  198. void* ConicCamera::GetExtensionDataBuffer(THIS) {
  199.   return &fData; // The Shell uses this pointer to set the values of the camera's parameters
  200.   }
  201.   
  202. HRESULT ConicCamera::ExtensionDataChanged(THIS) {
  203.   fQuickZoom=((NUM3D)fData.fZoomCoef)/10.0;
  204.   // the Zoom have changed, so recalculate the Zoom factor.
  205.   return NOERROR;
  206.   }
  207.  
  208. // This function is here, to allow you to create and use special resource.
  209. HRESULT ConicCamera::HandleEvent(THIS_ ULONG SourceID) {
  210.   return ResultFromScode(E_NOTIMPL);
  211.   }
  212.  
  213. short ConicCamera::GetResID(THIS) {
  214.   return 131; // This is the view ID in the resource file
  215.   }
  216.   
  217. // I3DExCamera methods :
  218. // set the coordinates Transformation values
  219. HRESULT ConicCamera::SetTransform(THIS_ TRANSFORM3D* transform) {
  220.   fTransform=*transform; // Copy the data of transform and not the pointer.
  221.   return NOERROR;
  222.   }
  223.                          
  224. // Ray Creation :
  225. BOOLEAN ConicCamera::CreateRay(THIS_ VECTOR2D* screenPosition, VECTOR3D* resultOrigin, VECTOR3D* resultDirection) {
  226.   VECTOR3D tempV;
  227.   
  228.   // Origin of the Ray is on the Screen in the Global Coordinates System
  229.   tempV[0]=(*screenPosition)[0]/fCoef/fQuickZoom;
  230.   tempV[1]=(*screenPosition)[1]/fCoef/fQuickZoom;
  231.   tempV[2]=0.0;
  232.   LocalToGlobal(&fTransform,&tempV,resultOrigin);
  233.                         
  234.   // Create the vector from the Center of projection to the screenPoint
  235.   tempV[2]=-kQuickFocal;
  236.   
  237.   LocalToGlobalVector(&fTransform,&tempV,resultDirection);
  238.   
  239.     NUM3D norm=sqrt(SQR((*resultDirection)[0]) + SQR((*resultDirection)[1]) + SQR((*resultDirection)[2]));
  240.   (*resultDirection)[0] /= norm;
  241.   (*resultDirection)[1] /= norm;
  242.   (*resultDirection)[2] /= norm; // the Direction vector must be normalized
  243.   
  244.   return TRUE;
  245.   }
  246.   
  247.   
  248. // 3D Projection :
  249. BOOLEAN ConicCamera::Project3DTo2D(THIS_ VECTOR3D* position, VECTOR2D* resultScreenPosition, NUM3D* resultDistanceToScreen) {
  250.   NUM3D temp;
  251.   
  252.   *resultDistanceToScreen=-(*position)[2]; // Distance to the screen (-z) in Point Unit
  253.             
  254.   temp=kQuickFocal*kQuick288*kQuickmmTo3DUnit;  // Focal length in Point Unit        
  255.   if ((*position)[2]!=temp) {
  256.     temp-=(*position)[2]; // Distance from the 3D Point to the Focal Point
  257.     (*resultScreenPosition)[0]=(*position)[0]*kQuickFocal/temp*fQuickZoom;
  258.     (*resultScreenPosition)[1]=(*position)[1]*kQuickFocal/temp*fQuickZoom;
  259.     // Conical projection
  260.     }
  261.   else {
  262.     (*resultScreenPosition)[0]=0.0;
  263.     (*resultScreenPosition)[1]=0.0;
  264.     // the Point is in the Focal Plane so it can't be projected on the screen
  265.     }
  266.  
  267.   (*resultScreenPosition)[0]*=fCoef*fCoef;
  268.   (*resultScreenPosition)[1]*=fCoef*fCoef; // Conversion in Point Unit
  269.   
  270.   if (*resultDistanceToScreen<=0.0)
  271.     return FALSE; // The point is behind the Camera
  272.   else
  273.     return TRUE;  // The point is in front of the Camera, it's visible.
  274.   }                                           
  275.   
  276. ULONG ConicCamera::GetPrimitiveID(THIS) {
  277.   return 0;
  278.   }
  279.  
  280. ULONG ConicCamera::Clip3D(THIS_ FACET3D* localFacet, VERTEX3D* localVertices,
  281.                                                                       FACET3D* cameraFacet, VERTEX3D* cameraVertices,
  282.                                                                           NUM3D* clipBox) {
  283.   return 0; /*** to do ***/
  284.   }
  285.  
  286.  
  287. BOOLEAN ConicCamera::ClipLine3D(THIS_ VECTOR3D* P1, VECTOR3D* P2)    {
  288.   return FALSE; /*** to do ***/
  289.     }
  290.